﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WallpaperControl
{
    /// <summary>
    /// Condition that changes the wallpaper at a given time.
    /// </summary>
    internal class CondTime : Condition
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="changeTime">The time when the wallpaper should be changed.</param>
        public CondTime(DateTime changeTime)
        {
            this.Time = changeTime;

            Program.ControlPanel.radioButtonTime.Checked = true;
            Program.ControlPanel.timePicker.Value = this.Time;
        }

        /// <summary>
        /// The ID of this condition.
        /// </summary>
        internal const int CondId = 1;

        /// <summary>
        /// The time when the wallpaper should be changed.
        /// </summary>
        private DateTime _time;

        internal override int Id
        {
            get
            {
                return CondTime.CondId;
            }
        }

        /// <summary>
        /// Gets or sets the time when the wallpaper should be changed.
        /// </summary>
        public DateTime Time
        {
            get
            {
                return this._time;
            }
            set
            {
                /*if (value.CompareTo(DateTime.Now) > 0 && value != null)
                {*/
                    this._time = value;
                /*}
                else
                {
                    throw new NotSupportedException();
                }*/
            }
        }

        /// <summary>
        /// Gets the time until the next wallpaper change.
        /// </summary>
        internal override TimeSpan RemainingTime
        {
            get
            {
                DateTime targetTime = this.Time;
                DateTime currentTime = DateTime.Now;

                return currentTime - targetTime;
            }
        }

        /// <summary>
        /// Gets or sets the value for this condition.
        /// </summary>
        internal override object Value
        {
            get
            {
                return this.Time;
            }
            set
            {
                this.Time = Convert.ToDateTime(value);
            }
        }

        /// <summary>
        /// Checks if the current condition is true and executes the wallpaper 
        /// change.
        /// </summary>
        internal override void Check()
        {
            while (Program.CurrentSession.IsActive)
            {
                // Check if time is reached
                if (this.Time.Hour == DateTime.Now.Hour && this.Time.Minute == DateTime.Now.Minute && this.Time.Second == DateTime.Now.Second)
                {
                    // Change the wallpaper
                    WallpaperManager.Instance.SetNextWallpaper();
                }
                // Sleep for performance saving
                System.Threading.Thread.Sleep(1000);
            }
        }

        internal override void ManualChangeCallback()
        {
        }

        internal override void StopSession()
        {
        }

        internal override void ApplyValue()
        {
            this.Time = Program.ControlPanel.timePicker.Value;
        }
    }
}
